package com.itheima.huikebackend.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.huikebackend.context.BaseContext;
import com.itheima.huikebackend.dto.CluePoolDTO;
import com.itheima.huikebackend.entity.ClueTrackRecord;
import com.itheima.huikebackend.entity.TbAssignRecord;
import com.itheima.huikebackend.entity.TbClue;
import com.itheima.huikebackend.mapper.AssignRecordMapper;
import com.itheima.huikebackend.mapper.TbActivityMapper;
import com.itheima.huikebackend.mapper.TbClueMapper;
import com.itheima.huikebackend.service.TbClueService;
import com.itheima.huikebackend.utils.DateUtils;
import com.itheima.huikebackend.vo.CluePageVO;
import com.itheima.huikebackend.vo.ClueSelectIdVO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class TbClueServiceImpl implements TbClueService {

    private final TbClueMapper tbClueMapper;
    private final TbActivityMapper tbActivityMapper;
    private final AssignRecordMapper assignRecordMapper;

    @Override
    public Map list(TbClue tbClue) {
        PageHelper.startPage(tbClue.getPageNum(),tbClue.getPageSize());
        Page<CluePageVO> page = tbClueMapper.selectList(tbClue);

        Map map = new HashMap();

        map.put("total",page.getTotal());
        map.put("rows",page.getResult());
        return map;
    }

    @Override
    public void create(TbClue tbClue) {
        //保存线索信息
       tbClue.setCreateTime(DateUtils.getNowDate());
       tbClue.setCreateBy( BaseContext.getCurrentName());
       tbClueMapper.insert(tbClue);

       //保存线索分配信息
        TbAssignRecord tbAssignRecord =new TbAssignRecord();
        tbAssignRecord.setAssignId(tbClue.getId());
        tbAssignRecord.setUserId(Long.valueOf(( BaseContext.getCurrentId())));
        tbAssignRecord.setUserName( BaseContext.getCurrentName());
        tbAssignRecord.setDeptId(Long.valueOf(BaseContext.getCurrentDeptId()));
        tbAssignRecord.setCreateBy(BaseContext.getCurrentName());
        tbAssignRecord.setCreateTime(DateUtils.getNowDate());
        assignRecordMapper.insert(tbAssignRecord);

        //根据线索池配置规则，更新线索失效时间



    }

    @Override
    public ClueSelectIdVO query(Long id) {
        ClueSelectIdVO clueSelectIdVO =  tbClueMapper.selectById(id);
        return clueSelectIdVO;
    }

    @Override
    public List<ClueTrackRecord> queryList(Long clueId) {
        List<ClueTrackRecord> clueTrackRecord = tbClueMapper.selectByCuleId(clueId);
        return clueTrackRecord;
    }

    @Override
    public Map poolList(CluePoolDTO cluePoolDTO) {
        PageHelper.startPage(cluePoolDTO.getPageNum(),cluePoolDTO.getPageSize());
        Page<TbClue> page = tbClueMapper.selectPoolList(cluePoolDTO);
        Map map =new HashMap();
        map.put("rows",page.getResult());
        map.put("total",page.getTotal());
        return map;
    }
}
